package com.frxuexi.repair.view

import android.text.Editable
import android.text.TextWatcher
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.repair.dapter.RepairAdapter
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/repair/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    val repairAdapter by lazy { RepairAdapter() }
    var flag=false//true:升序，false:降序
    val list= mutableListOf<RepairBeanItem>()
    var position:Int?=null
    override fun initData() {
        vm.repairsData.observe(this){
            it.forEach {
                vm.addRepairRoom(it)
            }
            vm.getRepairsRoom()
//            list.clear()
//            list.addAll(it)
//            repairAdapter.data.clear()
//            repairAdapter.addData(it)
//            repairAdapter.notifyDataSetChanged()
        }

        vm.repairByNumData.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        vm.repairByUnameData.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }
        vm.deleteRepairData.observe(this){
            if(it){
                repairAdapter.notifyItemRemoved(position!!)
            }
        }
        vm.repairsRoomData.observe(this){
            list.clear()
            list.addAll(it)
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        vm.deleteRepairRoomData.observe(this){
            ToastUtils.showLong("删除成功"+it)
        }

    }

    override fun initView() {
        binding.headRepair.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("维修管理")
            headerAdd.setOnClickListener {
                ARouter.getInstance().build("/repair/RepairAddActivity").navigation()
            }
        }
        binding.rvRepair.apply {
            adapter=repairAdapter
            layoutManager=LinearLayoutManager(this@RepairActivity)
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/repair/RepairDetailActivity").withString("rid",repairAdapter.data.get(position).id.toString()).navigation()
        }

        binding.repairOrder.setOnClickListener {
//            repairAdapter.data.clear()
            if(flag){
//                list.sortByDescending { it.repairTime }
                repairAdapter.data.sortByDescending { it.repairTime }
                binding.repairOrderiv.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_asc)
            }else{
//                list.sortBy { it.repairTime }
                repairAdapter.data.sortBy { it.repairTime }
                binding.repairOrderiv.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_down)
            }
//            repairAdapter.addData(list)
            repairAdapter.notifyDataSetChanged()
            flag=!flag
        }

        binding.btnSearch.setOnClickListener {
            val content = binding.repairSv.text.toString()
            if(content.startsWith("WX")){//工单查询
                vm.getRepairByNum(content)
            }else{//根据用户查询
                vm.getRepairByUname(content)
            }
        }

        binding.repairSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s.isNullOrEmpty()){
                    repairAdapter.data.clear()
                    repairAdapter.addData(list)
                    repairAdapter.notifyDataSetChanged()
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        val touchHelper = ItemTouchHelper(object : SimpleCallback(0, LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                position = viewHolder.adapterPosition
//                repairAdapter.notifyItemRemoved(position)
                vm.deleteRepair(repairAdapter.data.get(position!!).id!!)
                vm.deleteRepairRoom(repairAdapter.data.get(position!!))
            }

        })
        touchHelper.attachToRecyclerView(binding.rvRepair)
    }

    override fun onResume() {
        super.onResume()
        vm.getRepairs()
//        vm.getRepairsRoom()
    }
}